<?php
namespace app\admin\controller;
use think\Db;
class Useraddress extends Base
{
    //列表
    public function lists()
    {
        if(is_post()){
            $cur_page       = input('page');
            $search_type    = input('search_type');
            $is_default     = input('is_default');
            $keyword        = input('keyword');


            $sql = " SELECT [1] FROM user_address AS ua LEFT JOIN user AS u ON ua.user_id = u.id ";
            $param = [];
            $w = ['where'];
         
            if ($search_type!='100all'){
                if (! empty($keyword)) {
                    switch ($search_type) {
                        case 'name':
                            $w[] = ' and ua.name like ?';
                            $param[] = "%{$keyword}%";
                        break;
                        case 'phone':
                            $w[] = ' and ua.phone like ?';
                            $param[] = "%{$keyword}%";
                        break;
                    }
                }
            }

            if(!empty($min_time)){
                $w[] = ' and ua.create_time>?';
                $param[] = strtotime($min_time);
            }
            if(!empty($max_time)){
                $w[] = ' and ua.create_time<?';
                $param[] = strtotime($max_time);
            }


            if ($is_default != '100all') {
                $w[] = ' and ua.is_default = ?';
                $param[] = $status;
            }
            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }
            #分页
            $page_size = c('a.page_size');
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "u.name , ua.id , ua.name as addname , ua.phone as addphone , ua.address , ua.is_default , ua.create_time";
            $sql_list = str_replace("[1]",$field,$sql);

            $sql_list .= " order by id desc limit {$start},{$page_size}";
            

            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['is_default'] = c('type.is_default',$v['is_default']);
                $rslist[$k]['create_time'] = com_formate_time($v['create_time']);
                $rslist[$k]['action']  = com_btn_edit("/useraddress/editif/id/{$v['id']}");
            }
            $body_html = com_list_body($rslist, ['id','name','addname','addphone','address','is_default','create_time','action']);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
        }
        $d = [
            'is_default' => ['100all'=>'是否默认'] + c('type.is_default'),
        ];
        return view('',$d);
    }



    public function editif($id){

        if(is_post()){
          $d = post();
          $rule = [
              'name'    =>  'require',
              'phone'   =>  'require',
          ];
          $msg = [
              'name.require'    =>  '收货人姓名不能为空',
              'phone.require'   =>  '收货人电话不能为空',
          ];
          com_validate($rule, $msg, $d, true);

          Db::name("user_address")->update($d);
          com_check(['suc_msg'=>'修改成功!','suc_js'=>'alert|close']);
      }
      $info = Db::query( "SELECT * FROM user_address WHERE id = ?" , [$id] );
      return view('',['info'=>$info['0']]);
    }
}
